<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          
          <a-col :md="8" :sm="24">
            <a-form-item label="入库单号">
              <a-input v-model="queryParam.orderNumber" placeholder="请输入入库单号"/>
            </a-form-item>
          </a-col>
          
          <a-col :md="8" :sm="24">
            <a-form-item label="生产单号">
              <a-input v-model="queryParam.sourceOrderNumber" placeholder="请输入生产单号"/>
            </a-form-item>
          </a-col>
          
        
          
          <a-col :md="24" :sm="24">
            <a-form-item label="归属公司：">
              <FCOrgRadioGroup v-model="queryParam.targetOrgId"
                               :filter-types="[$businessEnum.ORG_TYPE_ENUM.COMPANY.id]"/>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => { queryParam = { targetOrgId :null };$refs.table.refresh(true) }">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    
    <div class="table-operator">
    </div>
    
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.code"
      :rowSelection="null"
    >

      <span slot="action" slot-scope="text, record">
        <a @click="$refs.addForm.show(record)">发货</a>
        <!--        <a-divider type="vertical"/>-->
        <!--        <a @click="">装箱清单</a>-->
        <a-divider type="vertical"/>
        <a @click="$refs.deliverOrderListModal.show(record)">发货记录</a>
        <a-divider type="vertical"/>
        <a @click="$refs.productDeliverRecord.show(record)">产品发货进度</a>
        <a-divider type="vertical" v-if="hasPerm('emsOrder:endFHOrder')"/>
        <a class="text-success hover:text-success" @click="handleCompletedOrder(record)"  v-if="hasPerm('emsOrder:endFHOrder')">完结</a>
      </span>
    </s-table>
    
    <add-form ref="addForm" @ok="handleOk"/>
    <deliver-order-list-modal ref="deliverOrderListModal"/>
    <product-deliver-record ref="productDeliverRecord"/>
  
  </a-card>
</template>

<script>
import {STable} from '@/components'
import {page} from '@/api/modular/main/emsorder/emsOrderManage'
import FCOrgRadioGroup from '@/components/FcOrgRadioGroup/FCOrgRadioGroup'
import addForm from './addForm'
import DeliverOrderListModal from '@/views/main/productStorehouse/productDeliverOrderCompleted/deliverOrderListModal'
import ProductDeliverRecord from '@/views/main/productStorehouse/productDeliverOrderInProgress/productDeliverRecord'
import {endFHOrder, endScOrder} from "@/api/modular/main/emsordermaterial/emsOrderMaterialManage";


export default {
  components: {
    ProductDeliverRecord,
    STable,
    FCOrgRadioGroup,
    addForm,
    DeliverOrderListModal,
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      columns: [       // 表头
        {
          title: '序号', width: '80px',
          dataIndex: '',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '生产单号',
          dataIndex: 'orderNumber',
        },
        {
          title: '归属子公司',
          dataIndex: 'targetOrgName',
        },
        {
          title: '录入时间',
          dataIndex: 'createTime',
        },
        {
          title: '排班时间',
          dataIndex: 'inProgressTime',
        },
        {
          title: '完结时间',
          dataIndex: 'completedTime',
        },
        {
          title: '创建人',
          dataIndex: 'createUserName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '350px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.produceOrder.id
        this.queryParam.isOverseasStore = 'N'
        this.queryParam.isInStoreCompleted = 1
        this.queryParam.isDeliverCompleted = 0
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      
      urlParam: {},
      columnsType: '',
      suppliers: [],
      orgs: [],
      viewState: '',
      materialType: '',
    }
  },
  
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    // 完结订单
    handleCompletedOrder(record) {
      console.log('record', record)
      this.$confirm({
        title: '系统提示',
        content: '是否确认完结？',
        onOk: () => {
          endFHOrder({
            orderId: record.orderId
          }).then(res => {
            if (res.success) {
              this.$message.info('完结成功')
              this.handleOk()
            } else {
              this.$message.error('完结失败：' + res.message)
            }
          })
        },
        onCancel() {
          console.log('Cancel');
        },
      });
    }
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
